package parkshop.pmcourse.service;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import parkshop.pmcourse.domain.Info;
import parkshop.pmcourse.mapping.InfoMapper;
import parkshop.pmcourse.service.inter.InfoSer;

/**
 * 标识Service层，该类能在 @Controller 层中自动装配
 */
@Service
public class InfoSerImpl implements InfoSer{
    private static final int MODIFYPASSSUC=0;//修改密码成功
    private static final int PASSERROE=1;//密码错误
    private static final int SySTEMERROE=2;//系统错误
    private static final Logger LOGGER = LoggerFactory.getLogger(InfoSerImpl.class);
    @Autowired
    private InfoMapper infoMapper;

    

  
    public Info login(Info user) {
        try {
            user= infoMapper.selectByPw(user.getUsername(),user.getPassword());
        } catch (Exception e) {
           LOGGER.error(e.getMessage());
           return null;
        }
		return user;
        
    }
    
	public boolean signup(Info user) {
		// TODO Auto-generated method stub
		 
	        try {
	        	Info temp=infoMapper.selectByName(user.getUsername());
	        	if(temp!=null&&temp.getUsername()!=null)
	        		return false;
	        	
	             infoMapper.insert(user.getUsername(), user.getPassword());
	             return true;
	        } catch (Exception e) {
	           LOGGER.error(e.getMessage());
	        }
	      return false;
	}
	
    
    public boolean checkPass(Integer id,String oldPass){
        int num=0;
        try {
            num=infoMapper.selectByOldPass(oldPass,id);
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
        }
        return num>0;
    }
	
	public Info getUserInfoByName(String userName) {
		// TODO Auto-generated method stub
		Info user=null;
		try{
			user=infoMapper.selectByName(userName);
			user.setPassword(null);
		}catch(Exception e){
			LOGGER.error(e.getMessage());
			System.out.println(e.getMessage());
		}
		return user;
	}
	public void addSession(HttpServletRequest request, Info info) {
		// TODO Auto-generated method stub
		
	}

	public void destroySession(HttpServletRequest request) {
		// TODO Auto-generated method stub
		
	}
}
